<?php
namespace App\V3\V3_2_7\Controllers;

use App\V3\V3_2_7\Services\CloudService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Services\CommonService;

class CloudController extends BaseController
{
    /**
     * 添加云盒设置
     */
    public function deviceAdd(Request $request)
    {
        $validator = Validator::make($data = $request->all(),[
            'device_id' =>'required|string|between:1,50',
            'device_pwd' =>'required|string|between:1,100',
        ],[
            'device_id.required' => '云盒ID必须填写',
            'device_id.between' => '云盒ID必须为 1~50 个字符',
            'device_pwd.required' => '云盒密码必须填写',
            'device_pwd.between' => '云盒密码必须为 1~100 个字符',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        //写入数据
        $mainId     = $this->getMainUserId();
        $json       = CloudService::deviceAdd($mainId,$data);

        return $json;
    }

    /**
     * 编辑设备，如果有变动，需要重置审核状态
     */
    public function deviceEdit(Request $request)
    {
        $validator = Validator::make($data = $request->all(),[
            'cd_id' =>'required|integer|between:1,10',
            'device_id' =>'required|string|between:1,50',
            'device_pwd' =>'required|string|between:1,100',
        ],[
            'cd_id.required' => '数据ID必须填写',
            'cd_id.between' => '数据ID长度必须为 1~10位数 之间',
            'device_id.required' => '云盒ID必须填写',
            'device_id.between' => '云盒ID必须为 1~50 个字符',
            'device_pwd.required' => '云盒密码必须填写',
            'device_pwd.between' => '云盒密码必须为 1~100 个字符',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        //更新数据，如果有变更，需重置为 待审核状态
        $mainId     = $this->getMainUserId();
        $json       = CloudService::deviceEdit($mainId,$data);

        return $json;
    }

    /**
     * 删除云盒设备
     */
    public function deviceDel(Request $request)
    {
        $validator = Validator::make($data = $request->all(),[
            'cd_id' =>'required|integer|between:1,10',
        ],[
            'cd_id.required' => '数据ID必须填写',
            'cd_id.between' => '数据ID长度必须为 1~10位数 之间',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        //更新数据，如果有变更，需重置为 待审核状态
        $mainId     = $this->getMainUserId();
        $json       = CloudService::deviceDel($mainId,$data);

        return $json;
    }

    /**
     * 云设备列表
     */
    public function deviceList(Request $request)
    {
        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'pageSize'      =>'required|integer|min:1',//页面大小
            'currentPage'   =>'required|integer|min:1',//当前页
            'device_id'     => 'between:0,50',//搜索设备id
        ],[
            'pageSize.required' => '参数`pageSize`丢失',
            'pageSize.integer'  => '参数`pageSize`仅能为数字',
            'pageSize.min'      => '页面展示数据最小为“1”条',
            'currentPage.required'  => '参数`currentPage`丢失',
            'currentPage.integer'   => '参数`currentPage`仅能为数字',
            'currentPage.min'       => '当前分页最小为第“1”页',
            'device_id.between'     => 'device_id长度必须为 0-50 位数',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        $mainId     = $this->getMainUserId();
        $modelList  = CloudService::deviceList($mainId,$data);

        return CommonService::success('success',$modelList->toArray());
    }

    /**
     * 云盒打印机列表
     */
    public function devicePrinterList(Request $request)
    {
        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'device_id'     => 'required|string|between:1,50',//搜索设备id
        ],[
            'device_id.required' => '参数`device_id`丢失',
            'device_id.string'   => '参数`device_id`仅能为字符',
            'device_id.between'  => 'device_id长度必须为 1-50 字符',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        $mainId     = $this->getMainUserId();

        $deviceInfo         = CloudService::deviceInfo($mainId,$data['device_id']);
        $devicePrinterList  = CloudService::devicePrinterList($mainId,$data['device_id']);
        $jsonData           = ['deviceInfo'=>$deviceInfo->toArray(),'devicePrinterList'=>$devicePrinterList->toArray()];

        return CommonService::success('success',$jsonData);
    }


    /**
     * 设备状态同步
     */
    public function deviceSync(Request $request)
    {
        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'device_id'     => 'required|string|between:1,50',//设备id
        ],[
            'device_id.required'    => '参数`device_id`丢失',
            'device_id.string'      => '参数`device_id`仅能为字符串',
            'device_id.between'     => 'device_id长度必须为 1-50 位数',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        //更新云盒及打印机状态
        $mainId     = $this->getMainUserId();
        $json       = CloudService::deviceSync($mainId, $data['device_id']);

        return $json;
    }

    /**
     * 云设备列表（管理员）
     */
    public function deviceListAdmin(Request $request)
    {
        //验证是否为超级管理员
        if($this->isSubAdmin() == false){
            return CommonService::fail('只有超级管理员，才有权访问！');
        }

        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'pageSize'      =>'required|integer|min:1',//页面大小
            'currentPage'   =>'required|integer|min:1',//当前页
            'device_id'     => 'between:0,50',//搜索设备id
        ],[
            'pageSize.required' => '参数`pageSize`丢失',
            'pageSize.integer'  => '参数`pageSize`仅能为数字',
            'pageSize.min'      => '页面展示数据最小为“1”条',
            'currentPage.required'  => '参数`currentPage`丢失',
            'currentPage.integer'   => '参数`currentPage`仅能为数字',
            'currentPage.min'       => '当前分页最小为第“1”页',
            'device_id.between'     => 'device_id长度必须为 0-50 位数',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        //结果
        $modelList  = CloudService::deviceListAdmin($data);
        return CommonService::success('success',$modelList->toArray());
    }


    /**
     * 云盒审核
     */
    public function deviceCheckAdmin(Request $request)
    {
        //验证是否为超级管理员
        if($this->isSubAdmin() == false){
            return CommonService::fail('只有超级管理员，才有权访问！');
        }

        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'main_id'       =>'required|integer|min:1',//主帐号id
            'device_id'     =>'required|string|between:1,50',//搜索设备id
            'device_status' =>'required|integer|in:1,2',//审核状态
        ],[
            'main_id.required'      => '参数`main_id`丢失',
            'main_id.integer'       => '参数`pageSize`仅能为数字',
            'main_id.min'           => '最小为 1 数',
            'device_id.required'    => '参数`device_id`丢失',
            'device_id.integer'     => '参数`device_id`仅能为数字',
            'device_id.between'     => 'device_id长度必须为 1-50 位字符',
            'device_status.required'    => '参数`device_status`丢失',
            'device_status.integer'     => '参数`device_status`仅能为数字',
            'device_status.in'          => 'device_status 只能在 [1,2] 之中',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        return CloudService::deviceCheckAdmin($data);
    }


    /**
     * 添加打印机任务，此接口不对外开放，只是内部测试如何添加打印使用示例
     */
    public function printerJobAdd(Request $request)
    {
        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'main_id'       =>'required|integer|min:1',//主帐号id
            'device_id'     =>'required|string|between:1,50',//搜索设备id
            'cdp_port'      =>'required|integer',//打印机端口
            'jobFile'       =>'required|file',//文件
        ],[
            'main_id.required'      => '参数`main_id`丢失',
            'device_id.required'    => '参数`device_id`丢失',
            'cdp_port.required'     => '参数`cdp_port`丢失',
            'jobFile.required'     => '参数`jobFile`丢失',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        //方法一，直接调用当前接口，传送打印文件过来
        $path = $request->file('jobFile')->store('printer');
        $jobFilePath    = public_path('uploads/'.$path);

        /*
        //方法二，内部调用方法
        //先把文件存储到本地，得到绝对路径，添加到打印任务里，
        //等打印任务完成，或者失败，马上删除本地文件
        $filePathFull   = '/webdata/website/zswl/smyc-test/public/uploads/printer/20240116/tt.docx';
        $pathInfo       = pathinfo($filePathFull);
        $ext            = $pathInfo['extension'];
        $fileSource     = file_get_contents($filePathFull);
        $jobFilePath    = CloudService::storeSourceFile($fileSource,$ext);
        */

        //配置
        $printerConfig                  = CloudService::$_PRINTER_CONFIG;
        $printerConfig['jobFile']       = $jobFilePath;
        $printerConfig['callbackUrl']   = ''; //正式上线后，再配置
        return CloudService::printerJobAdd($data['main_id'],$data['device_id'],$data['cdp_port'],$printerConfig);
    }


    /**
     * 打印任务列表
     */
    public function printerJobList(Request $request)
    {
        //参数验证，带条件搜索
        $validator = Validator::make($data = $request->all(), [
            'pageSize'      =>'required|integer|min:1',//页面大小
            'currentPage'   =>'required|integer|min:1',//当前页
            'device_id'     => 'between:0,50',//搜索设备id
        ],[
            'pageSize.required' => '参数`pageSize`丢失',
            'pageSize.integer'  => '参数`pageSize`仅能为数字',
            'pageSize.min'      => '页面展示数据最小为“1”条',
            'currentPage.required'  => '参数`currentPage`丢失',
            'currentPage.integer'   => '参数`currentPage`仅能为数字',
            'currentPage.min'       => '当前分页最小为第“1”页',
            'device_id.between'     => 'device_id长度必须为 0-50 位数',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        $mainId     = $this->getMainUserId();
        $modelList  = CloudService::printerJobList($mainId,$data);

        return CommonService::success('success',$modelList->toArray());
    }

    /**
     * 打印任务删除
     */
    public function printerJobDel(Request $request)
    {
        $validator = Validator::make($data = $request->all(),[
            'cdpj_id' =>'required|integer|between:1,10',
        ],[
            'cdpj_id.required' => '数据ID必须填写',
            'cdpj_id.between' => '数据ID长度必须为 1~10位数 之间',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        $mainId     = $this->getMainUserId();
        $json       = CloudService::printerJobDel($mainId,$data);

        return $json;
    }

    /**
     * 打印任务详情
     */
    public function printerJobDetail(Request $request)
    {
        $validator = Validator::make($data = $request->all(),[
            'cdpj_id' =>'required|integer|between:1,10',
        ],[
            'cdpj_id.required' => '数据ID必须填写',
            'cdpj_id.between' => '数据ID长度必须为 1~10位数 之间',
        ]);

        //验证报告
        if ($valErr = $validator->errors()->first()) {
            return CommonService::fail($valErr);
        }

        $mainId     = $this->getMainUserId();
        $jobModel   = CloudService::printerJobDetail($mainId,$data);

        return CommonService::success('success',$jobModel->toArray());
    }

    /**
     * 打印配置参数
     */
    public function printerConfig()
    {
        $mainId     = $this->getMainUserId();
        $configList = CloudService::printerConfig($mainId);
        return CommonService::success('success',$configList);
    }


}
